package com.cms.service.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cms.common.SiteUtil;
import com.cms.constant.UserCoinConstant;
import com.cms.dao.mapper.MemberUserMapper;
import com.cms.dao.mapper.UserChargeMapper;
import com.cms.exception.SystemDBException;
import com.cms.model.ActivityCommission;
import com.cms.model.TrAdminUser;
import com.cms.model.TrUser;
import com.cms.model.UserCharge;
import com.cms.model.UserCoinLog;
import com.cms.model.UserCommission;
import com.cms.service.MemberUserService;
import com.cms.service.UserCoinLogService;

@Service
public class MemberUserServiceImpl implements MemberUserService
{
	@Autowired
	private MemberUserMapper userMapper;

	@Autowired
	private UserCoinLogService userCoinLogService;
	
	@Autowired
	private UserChargeMapper userChargeMapper;
	
	@Override
	public void changeUserStatus(TrUser user) throws SystemDBException {
		 this.userMapper.changeUserStatus(user);
	}

	@Override
	public void registerUser(TrUser user) throws SystemDBException {
		this.userMapper.registerUser(user);
	}

	@Override
	public void updateUserInfo(TrUser user) throws SystemDBException {
		this.userMapper.updateUserInfo(user);
	}
	
	@Override
	public void reCharge(TrUser user,UserCoinLog log) throws SystemDBException {
		 userMapper.reCharge(user); 
		 TrAdminUser admin = SiteUtil.getCurrentUser();
		 Date date = new Date();
		 UserCharge charge = new UserCharge();
		 charge.setSuccessDate(date);
         charge.setChargeOrder(log.getCoinOrder());
         charge.setUid(user.getUid());
         charge.setUname(user.getLoginName());
         charge.setAccount(log.getCoin());
         charge.setActualAccount(log.getCoin());
         charge.setChargeDate(date);
         charge.setAduitId(admin.getId());
         charge.setChargeAccount(-1);
         charge.setIsDelete(0); 
         charge.setRemark(log.getRemark());
         charge.setChargeIp(SiteUtil.getActionAddr());
         charge.setStatus("3");//2 自动到账  3 充值成功
         charge.setUid(log.getUserId());
         charge.setUname(log.getUname());
         charge.setChargeType(0);//线下充值
         userChargeMapper.insertSelective(charge);
		 userCoinLogService.addUserCoinLog(log);
	}

	@Override
	public TrUser getUserInfoByUid(String uid) throws SystemDBException {
		return userMapper.getUserInfoByUid(uid);
	}

	@Override
	public void handlerUserCash(int status,double coin, TrUser user) throws Exception 
	{
		user.setFcoin(user.getFcoin()-coin);
		if(status==UserCoinConstant.WITHDRAWAL_FAILED)//提现失败
		{
			user.setCoin(user.getCoin()+coin);
		} 
		userMapper.handlerUserCash(user);
	}

	@Override
	public void handlerUserCharge(TrUser user) throws Exception {
         userMapper.handlerUserCharge(user);		
	}

	@Override
	public void saveActivityCommission(ActivityCommission commission)
			throws Exception {
		TrUser user  = this.userMapper.getUserInfoByUid(commission.getUid());
		double coin = commission.getChargeAmount()*commission.getCommissionRate();
		UserCoinLog coinLog = new UserCoinLog();
		coinLog.setCoinOrder(commission.getChargeOrder());
		coinLog.setActionUid(commission.getCreateUser());
		coinLog.setActionAddr(SiteUtil.getActionAddr());
		coinLog.setUid(user.getId());
		coinLog.setUserId(user.getUid());
		coinLog.setUname(user.getLoginName());
		coinLog.setCoin(coin);
		coinLog.setUserCoin(user.getCoin()+coin);
		coinLog.setFcoin(user.getFcoin());
		coinLog.setLiqType(UserCoinConstant.ACTIVITY_COMMISSION);
		coinLog.setRemark(UserCoinConstant.ACTIVITY_COMMISSION_STR);
		userMapper.saveActivityCommission(commission);
		userCoinLogService.addUserCoinLog(coinLog);
		user.setCoin(user.getCoin()+coin);
		this.userMapper.handlerUserCash(user);
		
		UserCommission comm = new UserCommission();
		comm.setCoinOrder(commission.getChargeOrder());
		comm.setCommType(UserCoinConstant.ACTIVITY_COMMISSION);
		comm.setCommRemark(UserCoinConstant.ACTIVITY_COMMISSION_STR);
		comm.setCommAmount(coin);
		comm.setUid(user.getUid());
		comm.setUname(user.getLoginName());
		comm.setCreateUser(commission.getCreateUser());
		comm.setModifyUser(comm.getModifyUser());
		userCoinLogService.saveUserCommission(comm);
	}

	@Override
	public void deleteActivityCommission(ActivityCommission commission)
			throws Exception {
		userMapper.deleteActivityCommission(commission);
	}

	@Override
	public TrUser getUserInfoById(int uid) throws Exception {
		return this.userMapper.getUserInfoById(uid);
	}

	@Override
	public TrUser getUserInfoByLoginName(String loginName) throws SystemDBException 
	{
		return userMapper.getUserInfoByLoginName(loginName);
	}

	@Override
	public void setUserOffline() throws Exception {
		
	}
}
